$(document).ready(function (){
    var lastResult;
    $(".getbuildinfo").click(function (){
        $("#buildinfo").slideUp();
        tech_id = $(this).attr("id");
        $("input[name=id]").val(tech_id);
        if (tech_id > 200) {
            $("input[name=count]").removeAttr("readonly");
        }
        else {
            $("input[name=count]").attr("readonly",true);
        }
        
        tech_name = $("#"+tech_id+" span.tech-name").text()
        $("#build-name").text(tech_name);
        $.getJSON("/ajax/buildinfo",{ "id" : tech_id }, function (result) {
            if ($("input[name=id]").val() > 200) {
                $("#build-level").text(result["level"]);
            }
            else {
                $("#build-level").text(result["level"]+1);
            }
            lastResult = result;
            
            $("#build-metal").text(result["metal"]);
            $("#build-crystal").text(result["crystal"]);
            $("#build-deuterium").text(result["deuterium"]);
            $("#build-energy").text(result["energy"]);
            $("#build-consumption").text(result["energy consumption"]);
            
            $("#buildinfo").slideDown();
        });
    });
    $("input[name=count]").change(function (){
        $("#build-metal").text(lastResult["metal"]*$(this).val());
        $("#build-crystal").text(lastResult["crystal"]*$(this).val());
        $("#build-deuterium").text(lastResult["deuterium"]*$(this).val());
    });
});